package com.leyou.cart.controller;

import com.leyou.cart.pojo.Cart;
import com.leyou.cart.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CartController {


    @Autowired
    private CartService cartService;

    @PostMapping
    public ResponseEntity<Void> addCart(@RequestBody Cart cart){

        cartService.addCart(cart);
        return ResponseEntity.status(201).build();
    }

    @GetMapping
    public ResponseEntity<List<Cart>> queryCarts(){
        List<Cart> carts = this.cartService.queryCarts();

        if (carts != null && 0!=carts.size()) {
            return ResponseEntity.ok(carts);
        }

        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @DeleteMapping("{id}")
    public ResponseEntity<Void> deleteCartById(@PathVariable("id")Long id){
        this.cartService.deleteCartById(id);
        return ResponseEntity.ok().build();
    }

    @PutMapping("increment/{id}")
    public ResponseEntity<Void> increment(@PathVariable("id")Long id){
        this.cartService.increment(id);
        return ResponseEntity.ok().build();
    }
}
